package com.gelapps.memogel.cards;

import com.gelapps.memogel.Constants;
import com.gelapps.memogel.Functions;
import com.gelapps.memogel.R;
import com.gelapps.memogel.password.FunctionsPass;

import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CardView extends Activity {

	 static int RESULTCODE = 2;
	 
	private String sKey;
		
	@Override
	 protected void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.card_view);
	  String retrievedData = "";
	  Bundle bundle = this.getIntent().getExtras();
	  if(bundle != null) {
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
          if (preferences.getBoolean(Constants.getPrefStringShowPass(), Constants.getPrefDefaultShowPass()))
          {
              ((ImageView) findViewById(R.id.imgViewCardCvvView)).setVisibility(View.GONE);
              ((TextView) findViewById(R.id.txtCardCvvView)).setInputType(InputType.TYPE_CLASS_NUMBER);
              ((ImageView) findViewById(R.id.imgViewCardNumberView)).setVisibility(View.GONE);
              ((TextView) findViewById(R.id.txtCardNumberView)).setInputType(InputType.TYPE_CLASS_NUMBER);
              ((ImageView) findViewById(R.id.imgViewCardPinView)).setVisibility(View.GONE);
              ((TextView) findViewById(R.id.txtCardPinView)).setInputType(InputType.TYPE_CLASS_NUMBER);
          }
          retrievedData = bundle.getString(Constants.getCardsName());
          this.sKey = bundle.getString(Constants.getKeyString());
          ((TextView) findViewById(R.id.txtCardNameView)).setText(retrievedData);
          CardsMaster card = CardsManager.getInstance().getCard(retrievedData);
          ((TextView) findViewById(R.id.txtCardNumberView)).setText(FunctionsPass.decryptString(this.sKey,card.getNumber()));
          ((TextView) findViewById(R.id.txtCardPinView)).setText(FunctionsPass.decryptString(this.sKey,card.getPin()));
          ((TextView) findViewById(R.id.txtCardCvvView)).setText(FunctionsPass.decryptString(this.sKey,card.getCVV()));;
          ((TextView) findViewById(R.id.txtCardPhoneView)).setText(card.getPhoneToBlock());
          ((TextView) findViewById(R.id.txtCardPhoneView)).setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Intent callIntent = new Intent(Intent.ACTION_CALL);
               callIntent.setData(Uri.parse("tel:+" + ((TextView) findViewById(R.id.txtCardPhoneView)).getText().toString().trim()));
               startActivity(callIntent );
                }
            });

          if ((!preferences.getBoolean(Constants.getPrefShowEmpty(), Constants.getPrefDefaultShowEmpty()))&&(Functions.isStringEmpty(card.getCircuits())))
              ((LinearLayout) findViewById(R.id.cardViewCircuits)).setVisibility(View.GONE);
          else
              ((TextView) findViewById(R.id.txtCardCircuitsView)).setText(card.getCircuits());
          if ((!preferences.getBoolean(Constants.getPrefShowEmpty(), Constants.getPrefDefaultShowEmpty()))&&(Functions.isStringEmpty(card.getExpiry())))
              ((LinearLayout) findViewById(R.id.cardViewExpiry)).setVisibility(View.GONE);
          else
              ((TextView) findViewById(R.id.txtCardExpiryView)).setText(card.getExpiry());
          if ((!preferences.getBoolean(Constants.getPrefShowEmpty(), Constants.getPrefDefaultShowEmpty()))&&(Functions.isStringEmpty(card.getBalance())))
              ((LinearLayout) findViewById(R.id.cardViewBalance)).setVisibility(View.GONE);
          else
              ((TextView) findViewById(R.id.txtCardBalanceView)).setText(card.getBalance());
          if ((!preferences.getBoolean(Constants.getPrefShowEmpty(), Constants.getPrefDefaultShowEmpty()))&&(Functions.isStringEmpty(card.getHolder())))
              ((LinearLayout) findViewById(R.id.cardViewHolder)).setVisibility(View.GONE);
          else
              ((TextView) findViewById(R.id.txtCardHolderView)).setText(card.getHolder());
          if ((!preferences.getBoolean(Constants.getPrefShowEmpty(), Constants.getPrefDefaultShowEmpty()))&&(Functions.isStringEmpty(card.getBillAddress())))
              ((LinearLayout) findViewById(R.id.cardViewBillAddress)).setVisibility(View.GONE);
          else
              ((TextView) findViewById(R.id.txtCardBillAddressView)).setText(card.getBillAddress());
          if ((!preferences.getBoolean(Constants.getPrefShowEmpty(), Constants.getPrefDefaultShowEmpty()))&&(Functions.isStringEmpty(card.getBank())))
              ((LinearLayout) findViewById(R.id.cardViewBank)).setVisibility(View.GONE);
          else
              ((TextView) findViewById(R.id.txtCardBankView)).setText(card.getBank());
          if ((!preferences.getBoolean(Constants.getPrefShowEmpty(), Constants.getPrefDefaultShowEmpty()))&&(Functions.isStringEmpty(card.getWebSite())))
              ((LinearLayout) findViewById(R.id.cardViewWebSite)).setVisibility(View.GONE);
          else
              ((TextView) findViewById(R.id.txtCardWebSiteView)).setText(card.getWebSite());
          if ((!preferences.getBoolean(Constants.getPrefShowEmpty(), Constants.getPrefDefaultShowEmpty()))&&(Functions.isStringEmpty(card.getNote())))
              ((LinearLayout) findViewById(R.id.cardViewNote)).setVisibility(View.GONE);
          else
              ((TextView) findViewById(R.id.txtCardNoteView)).setText(card.getNote());
       ((ImageView) findViewById(R.id.imgViewCardNumberView)).setOnClickListener(new View.OnClickListener()
          {
              @Override
              public void onClick(View view) {
                  if (((TextView) findViewById(R.id.txtCardNumberView)).getInputType()!=InputType.TYPE_CLASS_NUMBER)
                  {
                          ((ImageView) findViewById(R.id.imgViewCardNumberView)).setImageDrawable(getResources().getDrawable(R.drawable.secure2));
                          ((TextView) findViewById(R.id.txtCardNumberView)).setInputType(InputType.TYPE_CLASS_NUMBER);
                  }
                  else
                  {
                      ((ImageView) findViewById(R.id.imgViewCardNumberView)).setImageDrawable(getResources().getDrawable(R.drawable.not_secure2));
                      ((TextView) findViewById(R.id.txtCardNumberView)).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                  }
	              }
       });
          ((ImageView) findViewById(R.id.imgViewCardPinView)).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if (((TextView) findViewById(R.id.txtCardPinView)).getInputType()!=InputType.TYPE_CLASS_NUMBER)
                  {
                      ((ImageView) findViewById(R.id.imgViewCardPinView)).setImageDrawable(getResources().getDrawable(R.drawable.secure2));
                      ((TextView) findViewById(R.id.txtCardPinView)).setInputType(InputType.TYPE_CLASS_NUMBER);
                  }
                  else
                  {
                      ((ImageView) findViewById(R.id.imgViewCardPinView)).setImageDrawable(getResources().getDrawable(R.drawable.not_secure2));
                      ((TextView) findViewById(R.id.txtCardPinView)).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                  }
              }
          });
          ((ImageView) findViewById(R.id.imgViewCardCvvView)).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if (((TextView) findViewById(R.id.txtCardCvvView)).getInputType()!=InputType.TYPE_CLASS_NUMBER)
                  {
                      ((ImageView) findViewById(R.id.imgViewCardCvvView)).setImageDrawable(getResources().getDrawable(R.drawable.secure2));
                      ((TextView) findViewById(R.id.txtCardCvvView)).setInputType(InputType.TYPE_CLASS_NUMBER);
                  }
                  else
                  {
                      ((ImageView) findViewById(R.id.imgViewCardCvvView)).setImageDrawable(getResources().getDrawable(R.drawable.not_secure2));
                      ((TextView) findViewById(R.id.txtCardCvvView)).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                  }
              }
          });
       }
	  Bundle b = new Bundle();
	  b.putString("Value", retrievedData);
	  Intent intent = new Intent();
	  intent.putExtras(b);
	  setResult(RESULTCODE, intent);
	 }

/*					Il setting menu in questa activity non si deve vedere
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_card_view, menu);
		return true;
	}
*/
}
